package com.lzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.common.QueryPageParam;
import com.lzy.entity.Result;
import com.lzy.entity.Teacher;
import com.lzy.exception.CustomException;
import com.lzy.mapper.TeacherMapper;
import com.lzy.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.vo.TeacherWithClassVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-29
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {


    //日志对象
    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Result listPage(QueryPageParam query) {
        // 获取分页参数
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();

        log.info("使用MyBatis-Plus分页插件，pageNum: {}, pageSize: {}", pageNum, pageSize);
        log.info("查询参数: {}", query.getParam());

        // 创建分页对象
        Page<TeacherWithClassVO> page = new Page<>(pageNum, pageSize);

        // 调用Mapper方法查询
        HashMap<String, Object> param = (HashMap<String, Object>) query.getParam();
        IPage<TeacherWithClassVO> result = teacherMapper.selectByCondition(page, param);

        log.info("MyBatis-Plus分页结果 - 总记录数: {}, 当前页记录数: {}",
                result.getTotal(), result.getRecords().size());

        return Result.success(result.getRecords(), result.getTotal());
    }

    @Override
    public Result addTeacher(TeacherWithClassVO teacher) {
        teacherMapper.insertData();
        return Result.success();
    }

    @Override
    @Transactional
    public boolean updateTeacher(TeacherWithClassVO teacherVO) {
        if (teacherVO == null || teacherVO.getId() == null) {
            throw new CustomException("教师ID不能为空");
        }

        // 先查询数据库中已有的教师信息
        Teacher existingTeacher = this.getById(teacherVO.getId());
        if (existingTeacher == null) {
            throw new CustomException("教师不存在");
        }

        // 如果tNum为空或未修改,使用原来的编号
        if (teacherVO.getTNum() == null || teacherVO.getTNum().isEmpty()) {
            teacherVO.setTNum(existingTeacher.getTNum());
        }

        // 只有当编号发生变化时才检查唯一性
        if (!existingTeacher.getTNum().equals(teacherVO.getTNum())) {
            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teacher::getTNum, teacherVO.getTNum())
                    .ne(Teacher::getId, teacherVO.getId());// 排除当前教师ID
            
            if (this.getOne(queryWrapper) != null) {
                throw new CustomException("教师编号已存在，请更换");
            }
        }

        // 将VO对象转换为实体对象
        Teacher teacherToUpdate = new Teacher();
        BeanUtils.copyProperties(teacherVO, teacherToUpdate);
        
        // 执行更新操作
        return this.updateById(teacherToUpdate);
    }
}
